﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace OrderManage
{
    public class OrderService
    {
        //private List<Order> orders;
        /*public OrderService()
        {
            orders = new List<Order>();
        }*/
        /*public List<Order> GetOrders()
        {
            return orders;
        }*/
        public bool addOrder(Order order)
        {
            if (isUnique(order))
            {
                //orders.Add(order);
                using (var context=new OrderContext())
                {
                    var o = new OrderDb
                    {
                        OrderId = order.orderId,
                        customerName = order.customer.name,
                        orderDate = order.orderDate.ToString(),
                        money = order.GetMoney()
                    };
                    o.orderDetails = new List<OrderDetailDb>();
                    for(int i = 0; i < order.orderDetails.count; i++)
                    {
                        o.orderDetails.Add(new OrderDetailDb()
                        {
                            detailId = i + 1,
                            goodsName = order.orderDetails.goods[i].name,
                            goodsPrice=order.orderDetails.goods[i].price,
                            goodsNumber = order.orderDetails.numbers[i]
                        });
                        var queryGoods = context.Goods.Where(g=>g.name==order.orderDetails.goods[i].name);
                        if (queryGoods.Count() == 0)
                        {
                            GoodsDb goods = new GoodsDb()
                            {
                                name = order.orderDetails.goods[i].name,
                                price = order.orderDetails.goods[i].price
                            };
                            context.Goods.Add(goods);
                        }
                    }
                    var queryCus = context.Customers.Where(c=>c.name==order.customer.name);
                    if (queryCus.Count() == 0)
                    {
                        CustomerDb cus = new CustomerDb()
                        {
                            name = order.customer.name
                        };
                        context.Customers.Add(cus);
                    }
                    context.Orders.Add(o);
                    context.SaveChanges();
                }
                    return true;
            }
            else
            {
                return false;
            }
        }
        public bool isUnique(Order o)
        {
            /*foreach(Order item in orders)
            {
                if (o.orderId.Equals(item.orderId))
                {
                    return false;
                }
                if (o.customer.Equals(item.customer) && o.orderDate.year==item.orderDate.year&&o.orderDate.month==item.orderDate.month&&o.orderDate.day==item.orderDate.day )
                {
                    return false;
                }
            }
            return true;*/
            using (var context = new OrderContext())
            {
                var query1 = context.Orders.Where(p => p.OrderId==o.orderId);
                var query2 = context.Orders.Where(p=>p.customerName==o.customer.name/*&&p.orderDate==o.orderDate.ToString()*/);
                if (query1.Count() ==0 && query2.Count() ==0)
                {
                    return true;
                }
                return false;
            }
        }
        public void deleteOrder(string id)
        {
            OrderDb order = findID(id);
            if (order!=null)
            {
                //orders.Remove(order);
                using (var context = new OrderContext())
                {
                    context.Orders.Remove(order);
                    context.SaveChanges();
                }
                return;
            }
            throw new MyException("Not find the order.");
        }
        public void modifyCustomer(string id,string customerName) {
            OrderDb o = findID(id);
            using (var context=new OrderContext())
            {
                if (o != null)
                {
                    //o.customer = new Customer(customerName);
                    o.customerName = customerName;
                    var queryCus = context.Customers.Where(c=>c.name==customerName);
                    if (queryCus.Count() == 0)
                    {
                        CustomerDb cus = new CustomerDb() { name=customerName };
                        context.Customers.Add(cus);
                    }
                    context.SaveChanges();
                    return;
                }
            }
            throw new MyException("Not find the order.");
        }
        public void modifyDate(string id,string date)
        {
            OrderDb o = findID(id);
            /*if (o != null)
            {
                string y = date.Substring(0, date.IndexOf("-"));
                string m = date.Substring(date.IndexOf("-") + 1, date.LastIndexOf("-") - date.IndexOf("-") - 1);
                string d = date.Substring(date.LastIndexOf("-") + 1, date.Length - 1 - date.LastIndexOf("-"));
                o.orderDate = new Date(int.Parse(y), int.Parse(m), int.Parse(d));
                return;
            }*/
            using (var context=new OrderContext())
            {
                if (o != null)
                {
                    o.orderDate = date;
                    context.SaveChanges();
                }
            }
            throw new MyException("Not find the order.");
        }
        public void modifyOrderDetail(string id, int index, int num, double price)
        {
            OrderDb o = findID(id);
            /*if (o != null)
            {
                o.orderDetails.modifyGoods(index,num,price);
                return;
            }*/
            using (var context=new OrderContext())
            {
                var queryGoods = context.Goods.FirstOrDefault(g=>g.name==o.orderDetails[index].goodsName);
                queryGoods.price = price;
                o.orderDetails[index].goodsNumber = num;
                context.SaveChanges();
            }
            throw new MyException("Not find the order.");
        }
        public OrderDb findID(string id)
        {
            using (var context=new OrderContext())
            {
                var query = context.Orders.Where(o => o.OrderId==id);
                if (query.Count() == 0) { return null; }
                return query.ToList()[0];
            }
        }
        public List<OrderDb> findCustomer(string name)
        {
            //var query = orders.Where(o=>o.customer.name==name).OrderBy(o=>o.GetMoney());
            using (var context=new OrderContext())
            {
                var query = context.Orders.Where(o=>o.customerName==name).OrderBy(o=>o.money);
                return query.ToList();
            }
        }
        public List<OrderDb> findMoney(double money)
        {
            //var query = orders.Where(o => o.GetMoney() == money).OrderBy(o => o.orderId);
            using (var context=new OrderContext())
            {
                var query = context.Orders.Where(o => o.money == money).OrderBy(o=>o.OrderId);
                return query.ToList();
            }
        }
        public List<OrderDb> findOrderDate(string date)
        {
            //var query = orders.Where(o => o.orderDate.ToString()==date).OrderBy(o => o.GetMoney());
            using (var context = new OrderContext())
            {
                var query = context.Orders.Where(o => o.orderDate == date).OrderBy(o => o.money);
                return query.ToList();
            }
        }
        public List<OrderDb> findGoods(string name)
        {
            /*var query = orders.Where(o =>
            {
                foreach (Goods g in o.orderDetails.goods)
                {
                    if (g.name.Equals(name)) { return true; };
                }
                return false;
            }).OrderBy(o=>o.GetMoney());*/
            using (var context = new OrderContext())
            {
                var query = context.Details.Where(d =>d.goodsName==name);
                List<OrderDb> db = new List<OrderDb>();
                foreach(OrderDetailDb d in query)
                {
                    var queryOrder = context.Orders.FirstOrDefault(o=>o.OrderId==d.orderId);
                    int i = 0;
                    for( i = 0; i < db.Count(); i++)
                    {
                        if (db[i].OrderId == d.orderId)
                        {
                            break;
                        }
                    }
                    if (i ==db.Count())
                    {
                        db.Add(queryOrder);
                    }
                }
                var query1 = db.OrderBy(o=>o.money);
                return query1.ToList();
            }
        }
        /*public void sort()
        {
            orders.Sort();
        }*/
        /*public void sortWithMoney()
        {
            orders.Sort((o1,o2)=> o1.GetMoney().CompareTo(o2.GetMoney()));
        }*/
        /*public void sortWithGoodsNumber()
        {
            orders.Sort((o1,o2)=>o1.orderDetails.count-o2.orderDetails.count);
        }*/
        /*public void sortWithCustomer()
        {
            orders.Sort((o1, o2) => o1.customer.name.CompareTo(o2.customer.name));
        }*/
        /*public void sortWithDate()
        {
            orders.Sort((o1, o2) => {
                if (o1.orderDate.year - o1.orderDate.year == 0)
                {
                    if (o1.orderDate.month - o1.orderDate.month == 0)
                    {
                        return o1.orderDate.day - o1.orderDate.day;
                    }
                    return o1.orderDate.month - o1.orderDate.month;
                }
                return o1.orderDate.year - o1.orderDate.year;
            });
        }*/
    }
}
